package com.lmk.pet.controller;

import com.lmk.basic.exception.BusinessException;
import com.lmk.basic.util.AjaxResult;
import com.lmk.basic.util.PageList;
import com.lmk.pet.domain.SearchMasterMsg;
import com.lmk.pet.query.SearchMasterMsgQuery;
import com.lmk.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    /**
     * 接口：添加或修改
     * @param searchMasterMsg  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addorupdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult batchdel(@RequestBody List<Long> ids){
        try {
                searchMasterMsgService.batchdel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public SearchMasterMsg findOne(@PathVariable("id")Long id) {
        return searchMasterMsgService.findById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<SearchMasterMsg> findAll(){
        return searchMasterMsgService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryPage(query);
    }

    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            searchMasterMsgService.publish(searchMasterMsg,request);
            return new AjaxResult();
        }catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }  catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常");
        }
    }

    /*-------------------------------------消息接收-----------------------------------------------*/
    // 待审核或驳回：查询待审核或驳回的寻主消息 - 平台管理员才能查询 - 可以通过按钮权限限制，这里不做判断
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query){
        query.setState(0);
        return searchMasterMsgService.queryPage(query);
    }

    // 已完成：查询已完成的寻主消息 - 平台管理员才能查询 - 可以通过按钮权限限制，这里不做判断
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query){
        query.setState(2);
        return searchMasterMsgService.queryPage(query);
    }

    // 寻主池，查询审核通过还没有分配店铺或者被拒单的寻主消息 - state=1，ShopId为null
   /* @PostMapping("/toSearchMasterPool")
    public PageList<SearchMasterMsg> toSearchMasterPool(@RequestBody SearchMasterMsgQuery query){
        query.setState(1);
        //查询state=1，shopId is null的寻主消息
        return searchMasterMsgService.queryPage(query);
    }*/

    // 用户寻主列表：用于登录之后再个人中心查询自己所有状态的寻主消息
    @PostMapping("/user")
    public PageList<SearchMasterMsg> userSearchMasterMsg(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        return searchMasterMsgService.user(query,request);
    }

    // 待处理：查询审核通过并已经分配了店铺的寻主消息 - state=1，ShopId不为null
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        query.setState(1);
        return searchMasterMsgService.toHandle(query,request);
    }

    // 拒单
    @GetMapping("/reject/{id}")
    public AjaxResult publish(@PathVariable("id") Long msgId){
        try {
            searchMasterMsgService.reject(msgId);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"拒单失败");
        }
    }
    @PostMapping (value="/accept")
    public AjaxResult accept(@RequestBody Map<String,String> map){
        try {
            searchMasterMsgService.accept(map);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return  AjaxResult.me().setResultObj(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"接单失败");
        }
    }

}
